//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.jeecg.config.mybatis;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan({"org.jeecg.modules.**.mapper*","com.focusthings.common.service.**.mapper*","com.focusthings.pro.**.mapper*"})
public class MybatisPlusConfig {
    public static final String tenant_field = "tenant_id";
    private static final List<String> tenantTable = new ArrayList();
    private static final List<String> DDL_KEYWORD = new ArrayList();

    public MybatisPlusConfig() {
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = (new PaginationInterceptor()).setLimit(-1L);
        this.tenantConfig(paginationInterceptor);
        return paginationInterceptor;
    }

    private void tenantConfig(PaginationInterceptor paginationInterceptor) {
        List<ISqlParser> sqlParserList = new ArrayList();
        TenantSqlParser tenantSqlParser = new JeecgTenantParser();
        tenantSqlParser.setTenantHandler(new TenantHandler() {
            public Expression getTenantId(boolean select) {
                String tenant_id = TenantContext.getTenant();
                return new LongValue(tenant_id);
            }

            public String getTenantIdColumn() {
                return "tenant_id";
            }

            public boolean doTableFilter(String tableName) {
                return !MybatisPlusConfig.tenantTable.contains(tableName);
            }

            private Expression in(String ids) {
                InExpression inExpression = new InExpression();
                inExpression.setLeftExpression(new Column(this.getTenantIdColumn()));
                ExpressionList itemsList = new ExpressionList();
                List<Expression> inValues = new ArrayList(2);
                String[] var5 = ids.split(",");
                int var6 = var5.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String id = var5[var7];
                    inValues.add(new LongValue(id));
                }

                itemsList.setExpressions(inValues);
                inExpression.setRightItemsList(itemsList);
                return inExpression;
            }
        });
        sqlParserList.add(tenantSqlParser);
        paginationInterceptor.setSqlParserList(sqlParserList);
        paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
            public boolean doFilter(MetaObject metaObject) {
                String sql = (String)metaObject.getValue("delegate.boundSql.sql");
                Iterator var3 = MybatisPlusConfig.tenantTable.iterator();

                String tableName;
                String sql_lowercase;
                do {
                    if (!var3.hasNext()) {
                        return true;
                    }

                    tableName = (String)var3.next();
                    sql_lowercase = sql.toLowerCase();
                } while(sql_lowercase.indexOf(tableName.toLowerCase()) < 0);

                Iterator var6 = MybatisPlusConfig.DDL_KEYWORD.iterator();

                String key;
                do {
                    if (!var6.hasNext()) {
                        return false;
                    }

                    key = (String)var6.next();
                } while(sql_lowercase.indexOf(key) < 0);

                return true;
            }
        });
    }

    static {
        tenantTable.add("jee_bug_danbiao");
        DDL_KEYWORD.add("alter");
    }
}
